<template>
  <div class="login-container">
    <div class="login-form">
      <h2 class="login-title">股票系统登录</h2>
      <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" label-width="80px">
        <el-form-item label="账号" prop="email">
          <el-input v-model="loginForm.email" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="loginForm.password" type="password" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="large" style="width: 100%" @click="handleLogin">登录</el-button>
        </el-form-item>
        <el-form-item style="text-align: center;">
          <el-text type="primary" @click="toRegister" style="cursor: pointer;">还没有账号？去注册</el-text>
        </el-form-item>
      </el-form>
    </div>
    <!-- <el-upload
      class="avatar-uploader"
      :action="uploadUrl"
      :show-file-list="false"
      :on-success="handleAvatarSuccess"
      :auto-upload="false"
    >
      <el-button size="small" type="primary">点击上传头像</el-button>
    </el-upload> -->

  </div>
</template>

<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import useAuthStore from '@/stores/auth';
const router = useRouter();

const loginForm = ref({
  email: '',
  password: ''
});
const { userlogin } = useAuthStore();
const loginFormRef = ref(null);
const loginRules = {
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
  ]
};


const handleLogin = () => {
  if (!loginFormRef.value) {
    return;
  }
  loginFormRef.value.validate( async (valid, fields) => {
    if (valid) {
      userlogin(loginForm.value,()=>{
        router.push('/')
      });
    }
  })
};

const toRegister = () => {
  router.push('/register');
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f7fa;
}

.login-form {
  width: 400px;
  padding: 30px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.login-title {
  text-align: center;
  margin-bottom: 20px;
  color: #303133;
}
</style>